package com.chinasoft.rentalservice.mapper;

import com.chinasoft.ms.pojo.Rental;
import com.chinasoft.ms.pojo.RentalRecord;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface RentalMapper {

    @Select("SELECT * FROM rental WHERE id = #{rentalId}")
    Rental selectById(Long id);

    @Select("SELECT * FROM rental WHERE car_id = #{carId}")
    Rental selectByCarId(Long carId);

    @Select("SELECT r.id, r.car_id, r.user_id, r.rental_start, r.rental_end, r.extension_count, " +
            "rr.status, rr.rental_count " +
            "FROM rental r " +
            "LEFT JOIN rental_record rr ON r.id = rr.rental_id " +
            "WHERE r.car_id = #{carId} "+
            "ORDER BY r.id DESC LIMIT 1"
    )
    RentalRecord selectRecordByCarId(Long carId);

    @Select("select  * from rental_record where car_id = #{carId} ORDER BY rental_start DESC LIMIT 1")
    RentalRecord selectRecord2ByCarId(Long carId);
    @Select("<script>" +
            "SELECT r.id as rentalId, r.car_id, r.user_id, r.rental_start, r.rental_end, r.extension_count, " +
            "rr.status, rr.rental_count " +
            "FROM rental r " +
            "LEFT JOIN rental_record rr ON r.id = rr.rental_id " +
            "<where>" +
            "  <if test='rentalId != null'>AND r.id = #{rentalId}</if>" +
            "  <if test='carId != null'>AND r.car_id = #{carId}</if>" +
            "  <if test='userId != null'>AND r.user_id = #{userId}</if>" +
            "</where>" +
            "</script>")
    List<RentalRecord> selectRecordByCondition(RentalRecord query);

    @Insert("INSERT INTO rental(car_id, user_id, rental_start, rental_end, extension_count) " +
            "VALUES(#{carId}, #{userId}, #{rentalStart}, #{rentalEnd}, #{extensionCount})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Rental rental);

    @Insert("INSERT INTO rental_record(rental_id, car_id, user_id, rental_start, rental_end, status, rental_count) " +
            "VALUES(#{rentalId}, #{carId}, #{userId}, #{rentalStart}, #{rentalEnd}, #{status}, #{rentalCount})")
    @Options(useGeneratedKeys = true, keyProperty = "rentalId")
    int insertRecord(RentalRecord rentalRecord);

    @Update("UPDATE rental SET car_id = #{carId}, user_id = #{userId}, rental_start = #{rentalStart}, " +
            "rental_end = #{rentalEnd}, extension_count = #{extensionCount} " +
            "WHERE id = #{id}")  // 修改这里
    int update(Rental rental);

    @Update("UPDATE rental_record SET rental_id = #{rentalId}, car_id = #{carId}, user_id = #{userId}, " +
            "rental_start = #{rentalStart}, rental_end = #{rentalEnd}, status = #{status}, " +
            "rental_count = #{rentalCount} WHERE rental_id = #{rentalId}")
    int updateRecord(RentalRecord rentalRecord);

    @Delete("DELETE FROM rental WHERE id = #{id}")
    int deleteById(Long id);


    @Delete("<script>" +
            "DELETE FROM rental_record WHERE id IN " +
            "<foreach collection='list' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int deleteBatchRecords(@Param("list") List<Long> ids);


    @Delete("DELETE FROM rental_record WHERE rental_id = #{rentalId}")
    int deleteRecordById(@Param("rentalId") Long rentalId);

    @Update("UPDATE rental_record SET status = #{status} WHERE rental_id = #{rentalId}")
    int updateStatus(@Param("rentalId") Long rentalId, @Param("status") String status);

    @Update("UPDATE rental SET rental_end = #{date} WHERE id = #{id}")
    void updateRentalEnd(Long id, Date date);

    @Update("UPDATE rental_record SET rental_end = #{date} WHERE rental_id = #{id}")
    void updateRentalEnd2(Long id, Date date);
}
